<!--别忘记form 以及form的class-->
<form class="layui-form">
  <div id="xtree3" class="xtree_contianer"></div>
</form>

<script type="text/javascript">
  var xtree3;
  var parentCheckedValue = window.parent.permissionKey;
  layui.use(['jquery', 'layer', 'form'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    $.ajax({
      url: context + '/api/syspermission/list?tree=true',
      dataType: 'json',
      type: 'get',
      success: function (res) {
        var parentArr = listArr(res.data);
        xtree3 = new layuiXtree({
          elem: 'xtree3'                  //必填
          , form: form                    //必填
          , data: parentArr       //必填
          , isopen: false  //加载完毕后的展开状态，默认值：true
          , ckall: false    //启用全选功能，默认值：false
          ,showCheckbox:false
          , click: function (data) { //节点选中状态改变事件监听，全选框有自己的监听事件
             allCheckedCk = new Array();
             count =0;
       　
            //设置只能单选      
              var oCks = xtree3.GetAllCheckBox();
              // 循环数组，如果值不等于当前选中的值，将选中状态设为false
              for (var i = 0; i < oCks.length; i++) {
                if (oCks[i].value != data.value) {
                  $(oCks[i]).prop("checked", false)
                }
              }
        　　//根据当前节点的值获取一级节点的值  即当前顶级父节点的值 
       　　 getParent(data.elem.value);
            allCheckedCk.push(data.elem.value)
            title = data.elem.title;
            key = data.elem.value;
            keys = allCheckedCk.toString();
            form.render('checkbox');
          }
        });
      }
    });


  });
  var title;
  var key;
  var keys;
  var callbackdata = function(){
       var data ={title:title
        ,key:key,keys:keys};
        return data;
   }
//  获取顶级节点值的方法
var allCheckedCk;
var count;
function getParent(value) {
   var parent = xtree3.GetParent(value);
   if (parent == null) {
       $(parent).prop("checked", false);
       return;
   } else {
      allCheckedCk.push(parent.value);
      
      $(parent).prop("checked", true);
       getParent(parent.value);
   }
}
  function listArr(arr) {
    var root = new Array();
    if (arr != null || arr.length > 0) {
      for (var i = 0; i < arr.length; i++) {
        var item = {};
        item.title = arr[i].permissionName;
        item.value = arr[i].permissionKey;
        item.checked = item.value == parentCheckedValue;
        if (arr[i].children != null) {
          listChildArr(arr[i].children, item);
        }
        root[i] = item;
      }

    }
    return root;
  }
  function listChildArr(arr, parent) {
    parent.data = [];
    for (var i = 0; i < arr.length; i++) {
      var item = {};
      item.title = arr[i].permissionName;
      item.value = arr[i].permissionKey;
      if (arr[i].children != null) {
        listChildArr(arr[i].children, item);
      }
      item.checked = item.value == parentCheckedValue;
      //console.log(item);
      parent.data[i] = item;
    }

  } 
</script>